package com.peakmain.systemmoudle.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.peakmain.baselibrary.recyclerview.adapter.CommonRecyclerAdapter;
import com.peakmain.baselibrary.recyclerview.adapter.ViewHolder;
import com.peakmain.sysemmoudle.R;
import com.peakmain.systemmoudle.activity.SystemListActivity;
import com.peakmain.baselibrary.widget.FlowLayout;
import com.peakmain.baselibrary.adapter.BaseFlowAdapter;
import com.peakmain.wanandroid.netmodule.bean.system.SystemTreeBean;

import java.io.Serializable;
import java.util.List;

/**
 * author: peakmain
 * createdata：2019/7/16
 * mail: 2726449200@qq.com
 * desiption:
 */
public class SystemTreeLeftAdapter extends CommonRecyclerAdapter<SystemTreeBean> {
    private int mSelected;
    private FlowLayout mFlowLayout;

    public SystemTreeLeftAdapter(Context context, List<SystemTreeBean> data, FlowLayout flowLayout) {
        super(context, data, R.layout.recycler_view_system_left);
        this.mFlowLayout = flowLayout;
    }

    @Override
    public void convert(final ViewHolder holder, final SystemTreeBean item) {
        holder.setText(R.id.tv_name, item.getName());
        if (holder.getAdapterPosition() == mSelected) {
            holder.setBackground(R.id.ll_back, R.drawable.background);
            holder.setTextColor(R.id.tv_name, mContext.getResources().getColor(R.color.colorAccent));
            mFlowLayout.setAdapter(new BaseFlowAdapter() {
                @Override
                public int getCount() {
                    return item.getChildren().size();
                }

                @Override
                public View getView(final int position, ViewGroup parent) {
                    TextView tagTv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.item_system_flow_layout, parent, false);
                    tagTv.setText(item.getChildren().get(position).getName());
                    tagTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(mContext, SystemListActivity.class);
                            intent.putExtra("position",position);
                            intent.putExtra("children", (Serializable) item.getChildren());
                            mContext.startActivity(intent);
                        }
                    });
                    return tagTv;
                }
            });
        } else {
            holder.setBackground(R.id.ll_back, 0);
            holder.setTextColor(R.id.tv_name, mContext.getResources().getColor(R.color.color_9));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelected(holder.getAdapterPosition());
            }
        });

    }


    public void setSelected(int selected) {
        this.mSelected = selected;
        notifyDataSetChanged();
    }
}
